package com.shop.service.impl;

import com.shop.dao.ICartDao;
import com.shop.dao.IProductDao;
import com.shop.dao.impl.CartDaoImpl;
import com.shop.dao.impl.ProductDaoImpl;
import com.shop.entity.Cart;
import com.shop.entity.Product;
import com.shop.service.ICartService;

import java.util.List;

public class CartServiceImpl implements ICartService {

    private ICartDao cartDao = new CartDaoImpl();
    private IProductDao productDao = new ProductDaoImpl();

    /**
     * 查询购物车当中所有的购物车项，包括商品
     * @param uid
     * @return
     */
    @Override
    public List<Cart> findCartsByUid(int uid) {
        List<Cart> carts = cartDao.findCartsByUid(uid);
        for(Cart c : carts){
            Product product = productDao.findProductByPid(c.getPid());
            c.setProduct(product);
        }
        return carts;
    }

    /**
     * 通过登录的这个用户ID删除整个购物车信息
     * @param uid
     * @return
     */
    @Override
    public boolean clear(int uid) {
        if (cartDao.clear(uid)>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 删除一个商品
     * @param
     */
    @Override
    public boolean deleteOne(int cid) {
        if (cartDao.delteOne(cid)>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 通过id更新一个商品的数量
     * @param cid
     * @return
     */
    @Override
    public int update(int cid,int cnum,int ccount) {

        return cartDao.update(cid,cnum,ccount);

    }

    /**
     *添加购物车功能
     * @param cart
     * @return
     */
    @Override
    public boolean insert(Cart cart) {
        Product product = productDao.findProductByPid(cart.getPid());
        cart.setProduct(product);
        cart.setCcount(product.getPprice());
        if (cartDao.insert(cart)>0){
        return true;
        }else {
         return false;
        }
    }

}
